package cn.doitedu.hbase.bulk;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
import org.apache.hadoop.hbase.mapreduce.CellSortReducer;
import org.apache.hadoop.hbase.mapreduce.HFileOutputFormat2;
import org.apache.hadoop.hbase.mapreduce.KeyValueSortReducer;
import org.apache.hadoop.hbase.tool.BulkLoadHFilesTool;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

public class DemoHFile {

    public static class HFileMapper extends Mapper<LongWritable, Text, ImmutableBytesWritable, KeyValue> {
        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            String line = value.toString();
            String[] items = line.split(",", -1);
            ImmutableBytesWritable rowkey = new ImmutableBytesWritable(items[0].getBytes());

            KeyValue kv = new KeyValue(Bytes.toBytes(items[0]), Bytes.toBytes(items[1]), Bytes.toBytes(items[2]),
                    System.currentTimeMillis(), Bytes.toBytes(items[3]));
            if (null != kv) {
                context.write(rowkey, kv);
            }
        }
    }

    public static void main(String[] args) throws Exception {
        Configuration conf = new Configuration();
        conf.set("hbase.zookeeper.quorum", "hdp-01:2181,hdp-02:2181,hdp-03:2181");

        Job job = Job.getInstance(conf, "HFile bulk load test");
        job.setJarByClass(DemoHFile.class);

        job.setMapperClass(HFileMapper.class);
        //job.setReducerClass(KeyValueSortReducer.class);
        job.setReducerClass(CellSortReducer.class);

        job.setMapOutputKeyClass(ImmutableBytesWritable.class);
        job.setOutputValueClass(KeyValue.class);

        FileInputFormat.addInputPath(job, new Path(args[0]));
        FileOutputFormat.setOutputPath(job, new Path(args[1]));

        TableDescriptor td = TableDescriptorBuilder.newBuilder(TableName.valueOf("wc")).build();
        //HTableDescriptor td = new HTableDescriptor(TableName.valueOf("wc"));
        Connection conn = ConnectionFactory.createConnection(conf);
        RegionLocator regionLocator = conn.getRegionLocator(TableName.valueOf("wc"));

        HFileOutputFormat2.configureIncrementalLoad(job, td, regionLocator);

        boolean res = job.waitForCompletion(true);

        if (res) {
            Table table = conn.getTable(TableName.valueOf("wc"));
            Admin admin = conn.getAdmin();

            BulkLoadHFilesTool bulkLoadHFilesTool = new BulkLoadHFilesTool(conf);
            bulkLoadHFilesTool.doBulkLoad(new Path(args[1]), admin, table, regionLocator);
            //LoadIncrementalHFiles loader = new LoadIncrementalHFiles(conf);
            //loader.doBulkLoad(new Path(args[1]), admin, table, regionLocator);
        }

    }
}
